package com.example.nonggo.view.act;

import static android.view.View.GONE;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

import com.example.nonggo.Constants;
import com.example.nonggo.base.BaseMvpActivity;
import com.example.nonggo.contract.QuestionContract;
import com.example.nonggo.databinding.ActivityQuestionDetailBinding;
import com.example.nonggo.model.QuestionModel;
import com.example.nonggo.presenter.QuestionPresenter;
import com.example.nonggo.utils.PermissionUtils;
import com.tencent.mmkv.MMKV;

public class QuestionDetailActivity extends
        BaseMvpActivity<QuestionContract, QuestionPresenter, ActivityQuestionDetailBinding> implements QuestionContract {
    private static final String TAG = "QuestionDetailActivity";
    private String stringExtra;
    private final int REQUEST_RECORD_AUDIO_PERMISSION = 100;

    @Override
    protected void initData() {
//        startSSEConnection();
        presenter.getVerCode(stringExtra,
                MMKV.defaultMMKV().getString(Constants.FULL_NAME, "广东省-广州市-天河区"),
                MMKV.defaultMMKV().getString(Constants.MOBILE, ""));
    }

    @Override
    public void addNewQuestion(String question) {
        getBinding().tvQuestion.setText(question);
        presenter.getVerCode(question,
                MMKV.defaultMMKV().getString(Constants.FULL_NAME, "广东省-广州市-天河区"),
                MMKV.defaultMMKV().getString(Constants.MOBILE, ""));
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        stringExtra = getIntent().getStringExtra(Constants.QUESTION_TITLE);
        getBinding().tvQuestion.setText(stringExtra);
        getBinding().waveformView.setOnFinishListener(() -> presenter.stopRecordingAndTranscribe());
    }

    @Override
    public void showLoading() {
        getBinding().pbLoading.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideLoading() {
        getBinding().pbLoading.setVisibility(View.GONE);
    }

    @Override
    protected void initListener() {

        getBinding().btnKeepGoing.setOnClickListener(v -> {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
                PermissionUtils.requestPermissions(this, PermissionUtils.getPermissions(), REQUEST_RECORD_AUDIO_PERMISSION);
                return;
            }
            presenter.startRecording();
        });
        getBinding().micMaskLayout.setOnClickListener(v -> {
            presenter.stopRecordingAndTranscribe();
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_RECORD_AUDIO_PERMISSION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                presenter.startRecording();
            } else {
                // 处理权限被拒绝的情况
                Log.e(TAG, "录音权限被拒绝");
            }
            return;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected QuestionPresenter createPresenter() {
        return new QuestionPresenter(new QuestionModel());
    }

    @Override
    protected ActivityQuestionDetailBinding getViewBinding() {
        return ActivityQuestionDetailBinding.inflate(getLayoutInflater());
    }

    @Override
    public void showMicMask() {
        runOnUiThread(() -> getBinding().micMaskLayout.setVisibility(View.VISIBLE));
    }

    @Override
    public void hideMicMask() {
       runOnUiThread(() -> getBinding().micMaskLayout.setVisibility(View.GONE));
    }

    @Override
    public void updateWaveform(int db) {
        // 更新波形视图
        getBinding().waveformView.addVoiceSize(db);
    }

    @Override
    public void getQuestion(String txt) {
        runOnUiThread(() -> {
            getBinding().tvQuestionDetail.setText(txt); // 将 answer 内容追加到 TextView
            getBinding().pbLoading.setVisibility(GONE);
        });
    }

}
